﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chip8Dev
{
    public sealed class SystemMemory
    {
        /* Memory Size Constants */
        public const Int32 MemorySize = 4096 + 256;

        /* Memory */
        private Byte[] m_SystemBuffer;

        public SystemMemory()
        {
            m_SystemBuffer = new Byte[MemorySize];
        }

        public void Reset(Int32 romSize)
        {
            Array.Clear(m_SystemBuffer, romSize, m_SystemBuffer.Length - romSize);
        }

        public Byte[] SystemMemoryBuffer { get { return m_SystemBuffer;} }

        public void SaveState(Stream stateStream)
        {
            stateStream.Write(m_SystemBuffer, 0, m_SystemBuffer.Length);
        }

        public void RestoreState(Stream stateStream)
        {
            stateStream.Read(m_SystemBuffer, 0, m_SystemBuffer.Length);
        }

        public Int32 Size { get { return m_SystemBuffer.Length; }}

        public Int32 ReadInt16(Int32 address)
        {
            return (m_SystemBuffer[address + 0] << 8 |
                    m_SystemBuffer[address + 1]) & 0xFFFF;
        }
    }
}
